clear all 
* all files 
clear
local files : dir "data\in\dta\" files "*.dta"
di `files'

cd "data\in\dta"

foreach file in `files' {
di "`file'"
append using `file' 
}
recast byte noncomplier stratgroups hour 

* drop if ever used > 50 kwh in one hour 
replace forbruk_kwh = 0 if forbruk_kwh == . 
egen maxkwh = max(forbruk_kwh), by(maalepktnr)
drop if maxkwh > 50
drop maxkwh 

*gunique maalepktnr

* take out meters with 3 weeks consecutive 0s. 
fmerge m:1 maalepktnr using "data\in\meters_with_3week0s.dta", keepusing(maalepktnr) keep(master) nogenerate

fmerge m:1 Målepunktnr using "data\in\kunder_randomisert.dta", keep(match master) keepusing(treat) //nogenerate
tab _merge 
gunique maalepktnr
keep if _merge == 3
drop _merge 
gunique maalepktnr 


keep maalepktnr lnforbruk dato hour treat noncomplier tid stratgroups
g int year = year(dato)
g byte month = month(dato)
g byte day = day(dato)
keep if (year == 2019 & month == 12) | (year == 2020) 
keep if lnforbruk != .

sort maalepktnr tid 
drop if maalepktnr == maalepktnr[_n-1] & tid == tid[_n-1] & lnforbruk == lnforbruk[_n-1]
drop if maalepktnr == maalepktnr[_n-1] & tid == tid[_n-1] 
xtset maalepktnr tid 

merge m:1 dato hour using "data\in\temp_Timesdata2014_2020_inkllags", keep(match) nogen

label var lnforbruk "ln(kwh)"
label var hour "tid på døgnet"
label var hour "time of day"
g byte treatday = 0
  replace treatday = 1 if year == 2019 & month == 12 & day == 10
  replace treatday = 2 if year == 2019 & month == 12 & day == 19
  replace treatday = 3 if year == 2020 & month == 1 & day == 23
  replace treatday = 4 if year == 2020 & month == 1 & day == 30
  replace treatday = 5 if year == 2020 & month == 2 & day == 13
  replace treatday = 6 if year == 2020 & month == 2 & day == 26
  replace treatday = 7 if year == 2020 & month == 3 & day == 5
  replace treatday = 8 if year == 2020 & month == 3 & day == 31
  replace treatday = 9 if year == 2020 & month == 4 & day == 28
g trdate = dato if treatday != 0
g byte days_after = 0

drop trdate 
foreach d in 21893 21902 21937 21944 21958 21971 21979 22005 22033 {
  replace days_after = 1 if dato == `d' + 1 | dato == `d' + 2 
}

g byte Peak = inrange(hour,16,21)
g byte NonPeak = 1 - Peak
g byte EventDay = (treatday != 0)
g byte Treat = (treat=="Pris")
  drop treat 
g byte TPE = Treat*Peak*EventDay
g byte TNPE = Treat*NonPeak*EventDay
g byte TPnextdays = Treat*Peak*days_after
g byte TNPnextdays = Treat*NonPeak*days_after
g byte shoulder = inrange(hour,14,15) | inrange(hour,22,24)
g byte TsE = Treat*shoulder*EventDay
g byte NonPeakshoulder = 1 - max(Peak,shoulder)
g byte elcar = inrange(stratgroups,7,12) // elbil merged on navn eller etternavn og adresse

* Endogeneous variables 
g byte Effektpris = (Treat == 1 & noncomplier == 0 & EventDay == 1 & Peak == 1) | (Treat == 0 & noncomplier == 1 & EventDay == 1 & Peak == 1)
g byte EffektprisNonPeak = (Treat == 1 & noncomplier == 0 & EventDay == 1 & Peak == 0) | (Treat == 0 & noncomplier == 1 & EventDay == 1 & Peak == 0)
g byte Effektprisnext = (Treat == 1 & noncomplier == 0 & days_after == 1 & Peak == 1) | (Treat == 0 & noncomplier == 1 & days_after == 1 & Peak == 1)
g byte EffektprisNonPeaknext = (Treat == 1 & noncomplier == 0 & days_after == 1 & Peak == 0) | (Treat == 0 & noncomplier == 1 & days_after == 1 & Peak == 0)
g byte Effektprisshoulder = (Treat == 1 & noncomplier == 0 & EventDay == 1 & shoulder == 1) | (Treat == 0 & noncomplier == 1 & EventDay == 1 & shoulder == 1)


foreach y in TPE TNPE TPnextdays TNPnextdays Peak TsE shoulder Effektpris EffektprisNonPeak Effektprisnext EffektprisNonPeaknext Effektprisshoulder {
g byte `y'elcar = `y'*elcar 
}

g byte PeakxEvent = Peak*EventDay
g byte ShoulderxEvent = shoulder*EventDay

* estimere for alle events individuelt 
g treattimeline = .
  replace treattimeline = 1 if dato <= date("2019-12-10","YMD") + 2 
  replace treattimeline = 2 if dato <= date("2019-12-19","YMD") + 2 & treattimeline == .
  replace treattimeline = 3 if dato <= date("2020-01-23","YMD") + 2 & treattimeline == .
  replace treattimeline = 4 if dato <= date("2020-01-30","YMD") + 2 & treattimeline == .
  replace treattimeline = 5 if dato <= date("2020-02-13","YMD") + 2 & treattimeline == .
  replace treattimeline = 6 if dato <= date("2020-02-26","YMD") + 2 & treattimeline == .
  replace treattimeline = 7 if dato <= date("2020-03-05","YMD") + 2 & treattimeline == .
  replace treattimeline = 8 if dato <= date("2020-03-31","YMD") + 2 & treattimeline == .
  replace treattimeline = 9 if dato <= date("2020-04-28","YMD") + 2 & treattimeline == .

* per event  
forval i = 1/9 {
ivreghdfe lnforbruk c.temp##c.temp##c.temp temp_* Peak PeakxEvent ShoulderxEvent Peakelcar shoulder shoulderelcar ///
  (Effektpris EffektprisNonPeak Effektprisnext EffektprisNonPeaknext Effektpriselcar EffektprisNonPeakelcar Effektprisnextelcar EffektprisNonPeaknextelcar Effektprisshoulder Effektprisshoulderelcar =  TPE TNPE TPnextdays TNPnextdays TPEelcar TNPEelcar TPnextdayselcar TNPnextdayselcar TsE TsEelcar) if treattimeline == `i', absorb(maalepktnr dato) cluster(maalepktnr)
est store e`i'  
estadd ysumm  
}

estout e* using "output\table_C7_by event.txt", replace type ///
  cells(b( fmt(3)) se(par fmt(3)) _star) indicate(temp) mlabels() collabels(none) legend ///
  order(Effektpris EffektprisNonPeak Effektprisnext EffektprisNonPeaknext Peak PeakxEvent ShoulderxEvent Effektpriselcar EffektprisNonPeakelcar Effektprisnextelcar EffektprisNonPeaknextelcar Peakelcar Effektprisshoulder shoulder Effektprisshoulderelcar shoulderelcar) ///
  varlabels(TPE TP TNPE TNP) starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(26) ///
  stats(ymean r2 N_clust N, fmt(3 3 0 0 0)) style(tex)
